/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servelt;

import DatabaseAccessing.Customer;
import DatabaseAccessing.UserModelController;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

/**
 *
 * @author MaramZahran
 */
public class DisplayAllUser extends HttpServlet {

    UserModelController userModelController; // create instance from model that represent all data about users


    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet DisplayAllUser</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet DisplayAllUser at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * this methods used to get Display all user from
     * database by adding users into request and read
     * them into jsp page adminprofiles
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
    userModelController=new UserModelController();
        RequestDispatcher rd =null; // rd used to forward to Display all users jsp page
        rd=request.getRequestDispatcher("AdminDisplayProfiles.jsp");
        
       // List<Customer> allUsers=userModelController.loadAllCustomer();

         // JOptionPane.showMessageDialog(null, "fsfds");
        // System.out.print("hello");
       // for(Customer c :allUsers)
       // {
        //    System.out.print(c.getCustomerEmail());
       // }

        request.setAttribute("allusers", userModelController.loadAllCustomer());

         if( request.getParameter("type") !=null &&  request.getParameter("type").equals("remove"))
         {
          userModelController.removeCustomer(Integer.parseInt(request.getParameter("ID")));
          request.setAttribute("allusers", userModelController.loadAllCustomer());
         }
          else if( request.getParameter("type") !=null &&  request.getParameter("type").equals("details"))
         {
              rd=request.getRequestDispatcher("DisplayTransactions.jsp");
          request.setAttribute("CustomerTransaction", userModelController.getTransaction(Integer.parseInt(request.getParameter("ID"))));
         }
         // send list that contains all users to jsp page to display them
        
        rd.forward(request, response); // forward from servlet to DisplayAllUser
    

    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
